package com.sdtv.basemodule.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;

import com.billy.android.loading.Gloading;
import com.gyf.immersionbar.components.ImmersionFragment;
import com.sdtv.basemodule.mvp.IView;
import com.sdtv.basemodule.mvp.presenter.PresenterDispatch;
import com.sdtv.basemodule.mvp.presenter.PresenterProviders;
import com.sdtv.basemodule.utils.NetworkUtils;
import com.sdtv.basemodule.widget.gloading.StatusConstant;
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle3.LifecycleProvider;

import butterknife.ButterKnife;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.base
 * @ClassName: BaseFragment
 * @Description: 该类实现
 * @Author: 小郭同学
 * @CreateDate: 2019-07-09 15:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public abstract class BaseFragment extends ImmersionFragment implements IView {

    // 跟View
    private View rootView;
    public Context mContext;
    // presenter 注册
    private PresenterProviders mPresenterProviders;
    private PresenterDispatch mPresenterDispatch;

    /**
     * 状态显示View
     */
    private Gloading.Holder holder;

    /**
     * 标志位 标志已经初始化完成。
     */
    protected boolean isPrepared;
    /**
     * 标志位 fragment是否可见
     */
    protected boolean isVisible;

    /**
     * 是否用了懒加载
     */
    protected boolean isLazy;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置presenter等的实现
        isPrepared = true;
        ButterKnife.bind(this, rootView);
        mPresenterProviders = PresenterProviders.inject(this);
        mPresenterDispatch = new PresenterDispatch(mPresenterProviders);
        mPresenterDispatch.attachView(getActivity(), this);
        mPresenterDispatch.onCreatePresenter(savedInstanceState);
        initView();
        lazyLoad();
        initData();

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        } else {
            if (null != rootView.getParent()) {
                ((ViewGroup) rootView.getParent()).removeAllViews();
            }
        }
        return rootView;
    }


    /**
     * 设置布局文件ID
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 加载页面数据开始
     */
    public abstract void initData();

    /**
     * 初始化页面view
     */
    public abstract void initView();

    /**
     * 设置titlebar
     * 状态栏颜色
     */
    @Override
    public void initImmersionBar() {
//        ImmersionBar.with(this)
//                .fitsSystemWindows(true)
//                .keyboardEnable(true)
////                .statusBarColor(AppConfig.INSTANCE.getConfig().getStatusColor())
//                .init();
    }

    /**
     * 显示加载中Loading
     */
    @Override
    public void showLoading() {
        if (null == holder) {
            initGloadingView();
        }
        holder.showLoading();
    }

    /**
     * 隐藏加载view
     */
    @Override
    public void hideLoading() {
        if (null == holder) {
            initGloadingView();
        }
        holder.showLoadSuccess();
    }

    /**
     * 加载页面失败
     */
    @Override
    public void onError() {
        if (null == holder) {
            initGloadingView();
        }
        if (NetworkUtils.isConnected()) {
            // 网络连接正常--
            holder.showLoadFailed();
        } else {
            holder.showLoadingStatus(StatusConstant.STATUS_NETWORK_ERROR);
        }
    }

    /**
     * 显示提示信息
     */
    @Override
    public void showMessage() {

    }

    /**
     * 显示加载中View
     */
    @Override
    public void showEmptyView() {
        if (null == holder) {
            initGloadingView();
        }
        holder.showEmpty();
    }

    /**
     * 重新加载
     */
    @Override
    public void onLoadRetry() {

    }

    @Override
    public LifecycleProvider<Lifecycle.Event> getProvider() {
        return AndroidLifecycle.createLifecycleProvider(this);
    }

    @Override
    public void initGloadingView() {
        if (null == holder) {
            holder = Gloading.getDefault().wrap((Activity)
                    mContext).withRetry(() -> onLoadRetry());
        }
    }

    /**
     * 显示加载Dialog
     */
    @Override
    public void showLoadingDialog() {

    }

    /**
     * 隐藏加载Dialog
     */
    @Override
    public void hideLoadingDialog() {

    }

    /**
     * 懒加载
     */
    protected void lazyLoadData() {
        isLazy = true;
    }


    protected void loadData() {
        isLazy = false;
    }

    @Override
    public void onVisible() {
        lazyLoad();
    }

    @Override
    public void onInvisible() {
    }


    /**
     * 懒加载
     */
    private void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        lazyLoadData();
        isPrepared = false;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }


}
